using System;
using full_leaf_framework.Interact;
using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace touhou_bullet_rain;

public class PlayerController {

    private Level level;
    private Player controlledPlayer;

    private int controllerTick = 0;

    public bool isAvailable = false;

    public Player ControlledPlayer { get => controlledPlayer; }

    public PlayerController(Level level, Player controlledPlayer) {
        this.level = level;
        this.controlledPlayer = controlledPlayer;
        // 注：controlledPlayer可能为null，此时控制器不起作用
    }

    public void RequestCreateNewPlayer(string playerName, Vector2 pos, string uuid) {
        Player playerTarget = new Player(playerName, pos, uuid, level);
        level.Players.Add(uuid, playerTarget);
        controlledPlayer = playerTarget;
        isAvailable = true;
        level.LevelClient.RequestCreateNewPlayer(playerTarget);
    }

    /// <summary>
    /// 更新玩家的操作
    /// </summary>
    public void Update(GameTime gameTime, InputManager input, Camera camera) {
        controllerTick++;
        if (!isAvailable) { return; }
        if (input.GetTrackingKey(Keys.W).fired) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "f", "w"} );
            controlledPlayer.keyWPressed = true;
        }
        if (input.GetTrackingKey(Keys.A).fired) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "f", "a"} );
            controlledPlayer.keyAPressed = true;
        }
        if (input.GetTrackingKey(Keys.S).fired) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "f", "s"} );
            controlledPlayer.keySPressed = true;
        }
        if (input.GetTrackingKey(Keys.D).fired) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "f", "d"} );
            controlledPlayer.keyDPressed = true;
        }
        if (input.GetTrackingKey(Keys.W).released) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "r", "w"} );
            controlledPlayer.keyWPressed = false;
        }
        if (input.GetTrackingKey(Keys.A).released) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "r", "a"} );
            controlledPlayer.keyAPressed = false;
        }
        if (input.GetTrackingKey(Keys.S).released) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "r", "s"} );
            controlledPlayer.keySPressed = false;
        }
        if (input.GetTrackingKey(Keys.D).released) {
            level.LevelClient.AddInteractToBuffer("k", new string[3] { ControlledPlayer.uuid, "r", "d"} );
            controlledPlayer.keyDPressed = false;
        }
        // 对于按键进行处理，生成消息报头
        var mouse = input.GetTrackingMouse();
        Vector2 mouseActualPos = camera.ReturnPointerPos(mouse.pos.ToVector2());
        if (controllerTick % 3 == 0) {
            level.LevelClient.AddInteractToBuffer("m", new string[4]
                { ControlledPlayer.uuid, "p", mouseActualPos.X.ToString(), mouseActualPos.Y.ToString() } );
        }
        if (controllerTick % 25 == 0) {
            level.LevelClient.AddInteractToBuffer("e", new string[1] { GameManager.uuid } );
            DateTime utcNow = DateTime.UtcNow;
            DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan timeSpan = utcNow - unixStart;
            level.mostRecentInteractTime = (long)timeSpan.TotalMilliseconds;
        }
        if (mouse.firedLeft) {
            level.LevelClient.AddInteractToBuffer("m", new string[3] { ControlledPlayer.uuid, "f", "l" } );
        }
        if (mouse.releasedLeft) {
            level.LevelClient.AddInteractToBuffer("m", new string[3] { ControlledPlayer.uuid, "r", "l" } );
        }
        if (mouse.firedRight) {
            level.LevelClient.AddInteractToBuffer("m", new string[3] { ControlledPlayer.uuid, "f", "r" } );
            if (controlledPlayer.grazeColdDown < float.Epsilon) {
                controlledPlayer.grazeColdDown = controlledPlayer.grazeMaxColdDown;
            }
        }
        if (mouse.releasedRight) {
            level.LevelClient.AddInteractToBuffer("m", new string[3] { ControlledPlayer.uuid, "r", "r" } );
        }
        // 对于鼠标进行处理，生成消息报头
        // 在这里发出操作，操作加入level.InteractBuffer，并由其处理，发送到远程服务器
    }

}